package com.lzugis.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lzugis.common.R;
import com.lzugis.entity.StationInfo;
import com.lzugis.service.IStationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName StationInfoController
 * @Author lzugis
 * @Date 2020/9/16 11:32
 **/
@Api(tags = "站点管理")
@RestController
@RequestMapping("stationInfo")
public class StationInfoController {

    @Autowired
    private IStationInfoService stationInfoService;

    @ApiOperation("站点列表")
    @GetMapping("list")
    public R selectList() {
        return R.ok().put("list", stationInfoService.selectList(new EntityWrapper<>()));
    }

    @ApiOperation("根据Id获取站点信息")
    @GetMapping("select/{id}")
    public R selectById(@PathVariable String id) {
        return R.ok().put("data", stationInfoService.selectById(id));
    }

    @ApiOperation("根据Id删除站点")
    @GetMapping("delete/{id}")
    public R deleteById(@PathVariable String id) {
        return stationInfoService.deleteById(id) ? R.ok() : R.error();
    }

    @ApiOperation("插入站点")
    @PostMapping("insert")
    public R insert(@RequestBody StationInfo stationInfo) {
        return stationInfoService.insert(stationInfo) ? R.ok() : R.error();
    }

    @ApiOperation("更新站点")
    @PostMapping("update")
    public R update(@RequestBody StationInfo stationInfo) {
        return stationInfoService.updateById(stationInfo) ? R.ok() : R.error();
    }

    @ApiOperation("excel上传更新站点")
    @PostMapping("excel")
    public R insertExcel(@RequestParam Map<String, Object> para, HttpServletRequest request) throws Exception {
        MultipartFile multipartFile = ((MultipartHttpServletRequest) request).getFile("station");
        return stationInfoService.insertByExcel(multipartFile) ? R.ok() : R.error();
    }


}
